package com.yejing.poidemo;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo3 {
    public static void main(String[] args) throws IOException {

        Workbook workbook = new XSSFWorkbook(new FileInputStream("d:/IH电磁加热电脑饭煲整机综合评价报告2.xlsx"));
        XSSFSheet sheet0 = (XSSFSheet) workbook.getSheetAt(44);
        int index = 0;
        for (int i=0;i<3;i++){
            int r = 8 + index;
            writeACell(sheet0,r,3,"aa");
            writeACell(sheet0,r,4,"aaa");
            writeACell(sheet0,r,5,"aab");
            writeACell(sheet0,r,6,"aac");
            writeACell(sheet0,r,7,"aad");
            writeACell(sheet0,r,8,"aaec");
            writeACell(sheet0,r,9,"aaad");
            writeACell(sheet0,r,10,"agea");
            writeACell(sheet0,r,11,"agrra");
            writeACell(sheet0,r,12,"azbda");
            writeACell(sheet0,r,13,"awta");
            writeACell(sheet0,r,14,"asga");
            writeACell(sheet0,r,15,"aera");

            index +=1;
        }


        FileOutputStream os = new FileOutputStream("d://test.xlsx");
        workbook.write(os);
        System.out.println("finish");
    }

    public static void writeACell(XSSFSheet sheet0, int r, int c, String strValue) {
        if(StringUtils.isNotBlank(strValue)){
            XSSFRow row = sheet0.getRow(r);
            if(row == null) row = sheet0.createRow(r);
            XSSFCell cell = row.getCell(c);
            if(cell == null) cell = row.createCell(c);
            try{
                double v = Double.parseDouble(strValue);
                cell.setCellValue(v);
            }catch (Exception e){
                cell.setCellValue(strValue);
            }
        }
    }
}
